var Game = Class.extend({
	init: function(id,className){
		this.canvas = new Canvas('canvas');
		this.hero = new Hero (5, {x: 100, y: 100, width: 64, height: 64, 
			sprite: sprites.walk, 
			direction: 1
		}, className);
		this.id2 = id;
		this.map = Level1(this.canvas).map;
		this.monsters = Level1(this.canvas).monsters;
		this.keysDown = {};
		this.keysPress = {};
		this.miniMap = new MiniMap(sprites.miniMap, this.map.frames[0].length, this.map.frames.length);
		this.backgroudnObjects = Level1(this.canvas).backgroundObjects;
		this.infoBar = new InfoBar(1, 5, 5);
		this.player = new Player('Player 1');
		this.dropables = Level1(this.canvas).dropables;
		this.quests = Level1(this.canvas).quests;
		this.level1Completed = false;
		this.level2Completed = false;
		this.level3Completed = false;
		this.levelIsChanging = true;
		this.currentLevel = 1;
		this.isFinished = false;
		this.responseFinished = false;
		
	},
	load: function (){
		var loadedImages = 0;
		var loadID;
		var that = this;
		(function (){
			for (key in sprites){
				loadSpride(sprites[key])
			}
			
			loadID = setInterval(function(){
				if (loadedImages < 60){
					console.log('load');
					
				}else{
					clearInterval(loadID);
					that.listeners(that.keysDown, that.keysPress);
					that.id = setInterval(that.main, 50, that);
				}
			}, 80);
			
		})();
		
		function loadSpride (sprite){
			sprite.image = new Image();
			sprite.image.src = sprite.src;
			sprite.image.onload = function(){
				loadedImages++;
				sprite.isLoad = true;
			}
		}
		var that = this;
		setTimeout(function(){that.levelIsChanging = false;},2000);
	},
	listeners: function(keysDown, keysPress){
		document.addEventListener('keydown', function(e){
			if(e.keyCode>36 && e.keyCode<41) {
				e.preventDefault();
			}
			keysDown[e.keyCode] = true;
		}, false);

		document.addEventListener('keyup', function(e){
			if(e.keyCode>36 && e.keyCode<41) {
				e.preventDefault();
			}
			delete keysDown[e.keyCode];
		}, false);
		
		document.addEventListener('keypress', function(e){
			if(e.keyCode>36 && e.keyCode<41) {
				e.preventDefault();
			}
			keysPress[e.keyCode] = true;
		}, false);
	},
	update: function(){
		this.changeLevels();
		if(this.hero.isAlive){
			this.hero.update(this);
		}
		else {
			this.isFinished = true;
		}
		
		this.miniMap.update(this.keysPress, this.quests);
		
		for (var i = 0; i < this.quests.length; i++){
			this.quests[i].update(this.keysPress, this.hero, this.canvas, this.infoBar, this.map, this.backgroudnObjects[this.map.i][this.map.j]);
		}
		for(i in this.monsters[this.map.i][this.map.j]) {
			this.monsters[this.map.i][this.map.j][i].update(this.canvas,this.hero,this.player,
					this.monsters[this.map.i][this.map.j], this.dropables, this.backgroudnObjects[this.map.i][this.map.j]);
		}
		for (i in this.hero.casts){
			this.hero.casts[i].update(this.canvas.width, this.canvas.height,this.monsters[this.map.i][this.map.j]);
		}
		if(this.hero.isHitting) {
			for(i in this.monsters[this.map.i][this.map.j]) {
				this.hero.hit(this.monsters[this.map.i][this.map.j][i]);
			}
		}
	},
	render: function(){
		this.canvas.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
		var fgObjects = {};
		fgObjects = this.map.draw(this.canvas, this.hero, this.backgroudnObjects);
		
		//куеста се чертае само ако се намира на правилната част от картата
		for(var i = 0; i < this.quests.length; i++){
			if(this.quests[i].i === this.map.i && this.quests[i].j === this.map.j && !this.map.isChanging){
				if(MapObjects.prototype.isOnForeground.call(this.quests[i], this.hero)){
					fgObjects['q'+this.quests[i].id] = this.quests[i];
				}else{
					this.quests[i].draw(this.canvas);
				}
			}
		}			
		for (i in this.hero.casts){
			this.hero.casts[i].draw(this.canvas);
		}
		for (i in this.casts){
			this.monsters.casts[i].draw(this.canvas);
		}
		if(!this.map.isChanging) {
			for(i in this.monsters[this.map.i][this.map.j]) {	
				if(this.monsters[this.map.i][this.map.j][i].y>this.hero.y) {
					fgObjects['m'+this.monsters[this.map.i][this.map.j][i].id]=this.monsters[this.map.i][this.map.j][i];
				}
				else {
					this.monsters[this.map.i][this.map.j][i].draw(this.canvas);	
				}
			}
			
		}
		this.hero.draw(this.canvas);
		for (i in fgObjects){
			fgObjects[i].draw(this.canvas);		
		}
		
		this.infoBar.draw(this.canvas, this.hero);
		this.miniMap.draw(this.canvas, this.hero, this.map, this.quests);
		
		for(var i = 0; i < this.quests.length; i++){
			this.quests[i].drawOpenedQuest(this.canvas);
		}
		this.canvas.ctx.save();
		this.canvas.ctx.fillStyle = 'yellow';
		this.canvas.ctx.font="20px Georgia";
		this.canvas.ctx.fillText('Score : '+this.player.score,this.canvas.width-150,30);
		this.canvas.ctx.restore();
		if(this.levelIsChanging) {
			this.changeLevel(this.currentLevel);
		}
		if(this.isFinished) {
			this.end();
		}
	},
	changeLevels : function() {
		var that = this;
		if(!this.level1Completed) {
			if(this.map.i == 0 && this.map.j == 2 
				&& this.hero.x>370 && this.hero.x<420
				&& this.hero.y>100 && this.hero.y<150
				&& this.quests[0].isReturned
				&& !this.inBoss) {
				this.inBoss = true;
				this.quests = [new Quest(0, {x: 490, y: 100, height: 64, width: 64, sprite: sprites.quest, requirements: 1, i:0, j:3, item: 'key'})];
				this.backgroudnObjects = Level1(this.canvas).bossObjects;
				this.map = Level1(this.canvas).mapboss;
				this.monsters = Level1(this.canvas).boss;
			}
			if(this.hero.bossKilled) {
				this.level1Completed = true;
				this.inBoss = false;
				this.map = Level2(this.canvas).map;
				this.miniMap = Level2(this.canvas).miniMap;
				this.dropables = Level2(this.canvas).dropables;
				this.backgroudnObjects = Level2(this.canvas).backgroundObjects;
				this.monsters = Level2(this.canvas).monsters;
				this.quests = Level2(this.canvas).quests;
				this.map.i = 2;
				this.hero.x = 100;
				this.hero.y = 100;
				this.hero.bossKilled = false;
				this.levelIsChanging = true;
				this.currentLevel = 2;
				setTimeout(function(){that.levelIsChanging = false;},2000);
			}
		}
		if(this.level1Completed && !this.level2Completed) {
			if(this.map.i == 0 && this.map.j == 0 
				&& this.hero.x<20
				&& this.hero.y>300 && this.hero.y<400
				&& this.quests[0].isReturned
				&& !this.inBoss) {
				this.inBoss = true;
				this.quests = [new Quest(0, {x: 490, y: 100, height: 64, width: 64, sprite: sprites.quest, requirements: 1, i:0, j:3, item: 'key'})];
				this.backgroudnObjects = Level2(this.canvas).bossObjects;
				this.map = Level2(this.canvas).mapboss;
				this.monsters = Level2(this.canvas).boss;
			}
			if(this.hero.bossKilled) {
				this.level2Completed = true;
				this.inBoss = false;
				this.map = Level3(this.canvas).map;
				this.miniMap = Level3(this.canvas).miniMap;
				this.dropables = Level3(this.canvas).dropables;
				this.backgroudnObjects = Level3(this.canvas).backgroundObjects;
				this.monsters = Level3(this.canvas).monsters;
				this.quests = Level3(this.canvas).quests;
				this.map.j = 2;
				this.hero.x = 500;
				this.hero.y = 300;
				this.hero.bossKilled = false;
				this.levelIsChanging = true;
				this.currentLevel = 3;
				setTimeout(function(){that.levelIsChanging = false;},2000);
				
			}
		}
		if(this.level1Completed && this.level2Completed && !this.level3Completed) {
			if(this.quests[0].isReturned
				&& !this.inBoss) {
				this.quests = [new Quest(0, {x: 490, y: 100, height: 64, width: 64, sprite: sprites.quest, requirements: 1, i:0, j:3, item: 'key'})];
				this.backgroudnObjects = Level3(this.canvas).bossObjects;
				this.map = Level3(this.canvas).mapboss;
				this.monsters = Level3(this.canvas).boss;
				this.hero.x = 200;
				this.hero.y = 400;
			}
			if(this.hero.bossKilled) {
				this.isFinished = true;
			}
		}
	},
	selectItem: (function(){
		var droppedItems = 0;
		
		return function(x, y, dropables, container){
			var rand = Math.random();
			var item = null;
			
			if (rand > 0.2 && dropables.length > 0){
				var index = Math.floor(Math.random() * dropables.length);
				
				item = new BonusObjects('dropped' + droppedItems++, {x: x, y: y, type: dropables.splice(index, 1)});
			}
			
			return item;
		}
	})(),
	changeLevel : function(n) {
		this.canvas.ctx.save();
		this.canvas.ctx.fillStyle = "rgba(0,0,0,0.6)";
		this.canvas.ctx.fillRect(0,0,this.canvas.width,this.canvas.height);
		this.canvas.ctx.fillStyle = "white";
		this.canvas.ctx.font="50px Georgia";
		this.canvas.ctx.fillText('Level '+n,this.canvas.width/2-100,this.canvas.height/2);
		this.canvas.ctx.restore();
	},
	end : function() {
		this.canvas.ctx.save();
		this.canvas.ctx.fillStyle = "rgba(0,0,0,0.6)";
		this.canvas.ctx.fillRect(0,0,this.canvas.width,this.canvas.height);
		this.canvas.ctx.fillStyle = "white";
		this.canvas.ctx.font="50px Georgia";
		this.canvas.ctx.fillText('Game over',this.canvas.width/2-200,this.canvas.height/2-100);
		this.canvas.ctx.fillText('Your score is : '+this.player.score,this.canvas.width/2-200,this.canvas.height/2);
		this.canvas.ctx.restore();
		var url = 'http://ittalentsapi.bashibozuk.eu/game/game-end?id='+this.id2+'&X-GameID=103&score='+this.player.score;
		if(!this.responseFinished) {
			$.ajax(url, {
				method: 'GET',
				crossDomain : true,
				success: function(data) {
					console.log(data);
				}
			});
			this.responseFinished = true;
		}
	},
	dropItem: function (item, container){
		if(item){
			container[item.id] = item;
		}
		
	},
	main: function(that){
		that.update();
		that.render();
	}
});
